﻿






// *********** 返回LED8客显屏ESC指令输出字符串命令类*******************************
//1.串口屏设置波特率位2400 校验位：无 数据位：8 bit 停止位：1 bit
//****
//****
//****使用*********例如 serialPort1.Write(LED8Display.GetSendNum("12345678"));*****
//****使用*********例如 serialPort1.Write(LED8Display.GetOneStateFlgLight(2));*****
//****使用*********例如 serialPort1.Write(LED8Display.GetNeedStateFlgLight(true,false,true,true));*****
//****使用*********例如 serialPort1.Write(LED8Display.GetClearAllStr());*****
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WHControlLib.HardWare
{


    /// <summary>
    ///  返回LED8客显屏ESC指令输出字符串命令类
    /// </summary>
    public class LED8Display
    {

      bool LED8IsNumber(string str)
        {
            Regex regex = new System.Text.RegularExpressions.Regex(@"^[-]?[0-9]+[0-9]*[.]?[0-9]*$");
            bool b = regex.IsMatch(str);
            if (b)
            {
                return true;
            }
            else return false;
        }

        /// <summary>
        /// 返回想led8发送的数字字符串已经判断是否是数字和满足长度条件不满足返回8个负号（自动清屏）
        /// </summary>
        /// <param name="num">要显示的数字无小数点不能大于8位有小数点不能大于15位</param>
        /// <returns>led8发送的数字字符串</returns>
        public string GetSendNum(string num)
        {
           
                    StringBuilder sb = new StringBuilder();
            sb.Append((char)27);
            sb.Append((char)81);
            sb.Append((char)65); 
           
            if (num.Length>15)
            {
                num = "--------";


                sb.Append(num);
            sb.Append((char)13);
            return sb.ToString();
            }
            if (num.Contains(".")==false)
            {
                if (num.Length>8)
                {

                    num = "--------";
                    sb.Append(num);
                    sb.Append((char)13);
                    return sb.ToString();

                }
            }
            if (LED8IsNumber(num)&& num.Length>0)
            {
                   sb.Append(num);
                sb.Append((char)13);
                    return sb.ToString();
            }
            else
            {

                num = "--------";
                sb.Append(num);
                sb.Append((char)13);
                return sb.ToString();
            }

       
        }
        /// <summary>
        /// 返回清除屏幕数字显示不包括状态灯的字符串
        /// </summary>
        /// <returns></returns>
        public string GetClearNum()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append((char)24);

            return sb.ToString();
        }
        /// <summary>
        /// 返回设置那个状态灯亮其余全灭的字符串
        /// </summary>
        /// <param name="LightNum">在1和4 之间取值</param>
        /// <returns></returns>
        public string GetOneStateFlgLight(int LightNum)
        {
            if (LightNum > 0 && LightNum < 5)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append((char)27);
                sb.Append((char)115);

                sb.Append((char)(LightNum + 48));
                return sb.ToString();
            }
            else return null;

        }
        /// <summary>
        /// 获得需要那个状态灯亮的字符串
        /// </summary>
        /// <param name="d1">单价灯状态</param>
        /// <param name="d2">总计灯状态<</param>
        /// <param name="d3">收款灯状态<</param>
        /// <param name="d4">找零灯状态<</param>
        /// <returns>获得需要那个状态灯亮的字符串</returns>
        public string GetNeedStateFlgLight(bool d1,bool d2,bool d3,bool d4 )
           
        {
            char ON = (char)49;
            char OFF = (char)48;
            StringBuilder sb = new StringBuilder();
            sb.Append((char)2);
            sb.Append((char)76);
            char[]  LightAarray=   new char[4];
            LightAarray[0]= d1== true ?  ON:OFF;
            LightAarray[1] = d2 == true ? ON : OFF;
            LightAarray[2] = d3 == true ? ON : OFF;
            LightAarray[3] = d4 == true ? ON : OFF;
            for (int i = 0; i < 4; i++)
            {
            sb.Append(LightAarray[i]);
            }
          
            return sb.ToString();


        }
        /// <summary>
        ///清空屏幕即屏幕不显示任何
        /// </summary>
        /// <returns>获得请屏字符串</returns>
        public string GetClearAllStr()
        {
          char[] clr = new char[] { (char)12 };
            string CLR = new string(clr);
            return CLR;
        }
        /// <summary>
        /// 恢复LED8到上电开机时的状态。
        /// </summary>
        /// <returns></returns>
        public string GetLED8IniStr()
        {
            char[] LEDIni = new char[] { (char)27, (char)64 };
            string lEDini = new string(LEDIni);
            return lEDini;
        }

    }
}
